{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "emerging-theta",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36msqrtplus1\u001b[39m"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def sqrtplus1(x: Int) = {//在任何地方都可以使用导入\n",
    "  import scala.math.sqrt\n",
    "  sqrt(x) + 1.0\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "abstract-sandwich",
   "metadata": {},
   "outputs": [],
   "source": [
    "//如果存在命名冲突并且你需要从项目的根目录导入，请在包名称前加上 _root_：\n",
    "//import users.{UserPreferences => UPrefs}  // 导入类并且设置别名\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "norman-timer",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(console):3:1 expected end-of-input\r\n",
      "//包对象\n",
      "\n",
      "package gardening.fruits\n",
      "\n",
      "case class Fruit(name:String,color:String)\n",
      "object Apple extends Fruit(\"Apple\",\"green\")\n",
      "object Plum extends Fruit(\"Plum\",\"Blue\")\n",
      "object Banana extends Fruit(\"Banana\",\"yellow\")\r\n",
      "       ^"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "(console):3:1 expected end-of-input\r\n//包对象\n\npackage gardening.fruits\n\ncase class Fruit(name:String,color:String)\nobject Apple extends Fruit(\"Apple\",\"green\")\nobject Plum extends Fruit(\"Plum\",\"Blue\")\nobject Banana extends Fruit(\"Banana\",\"yellow\")\r\n       ^"
     ]
    }
   ],
   "source": [
    "//包对象\n",
    "\n",
    "package gardening.fruits\n",
    "\n",
    "case class Fruit(name:String,color:String)\n",
    "object Apple extends Fruit(\"Apple\",\"green\")\n",
    "object Plum extends Fruit(\"Plum\",\"Blue\")\n",
    "object Banana extends Fruit(\"Banana\",\"yellow\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "urban-christmas",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(console):1:1 expected end-of-input\r\n",
      "package gardening.fruits\n",
      "\n",
      "case class Fruit(name: String, color: String)\n",
      "object Apple extends Fruit(\"Apple\", \"green\")\n",
      "object Plum extends Fruit(\"Plum\", \"blue\")\n",
      "object Banana extends Fruit(\"Banana\", \"yellow\")\r\n",
      "^"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "(console):1:1 expected end-of-input\r\npackage gardening.fruits\n\ncase class Fruit(name: String, color: String)\nobject Apple extends Fruit(\"Apple\", \"green\")\nobject Plum extends Fruit(\"Plum\", \"blue\")\nobject Banana extends Fruit(\"Banana\", \"yellow\")\r\n^"
     ]
    }
   ],
   "source": [
    "package gardening.fruits\n",
    "\n",
    "case class Fruit(name: String, color: String)\n",
    "object Apple extends Fruit(\"Apple\", \"green\")\n",
    "object Plum extends Fruit(\"Plum\", \"blue\")\n",
    "object Banana extends Fruit(\"Banana\", \"yellow\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "found-parking",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(console):1:1 expected end-of-input\r\n",
      "package gardening\n",
      "package object fruit {\n",
      "    val planted = List(Apple,Plum,Banana)\n",
      "    def showFruit(fruit:Fruit):Unit = {\n",
      "        println(s\"${fruit.name}s are ${fruit.color\"})\n",
      "    }\n",
      "}\r\n",
      "^"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "(console):1:1 expected end-of-input\r\npackage gardening\npackage object fruit {\n    val planted = List(Apple,Plum,Banana)\n    def showFruit(fruit:Fruit):Unit = {\n        println(s\"${fruit.name}s are ${fruit.color\"})\n    }\n}\r\n^"
     ]
    }
   ],
   "source": [
    "package gardening\n",
    "package object fruit {\n",
    "    val planted = List(Apple,Plum,Banana)\n",
    "    def showFruit(fruit:Fruit):Unit = {\n",
    "        println(s\"${fruit.name}s are ${fruit.color\"})\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "biblical-oliver",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "cmd2.sc:1: not found: value gardening\n",
      "import gardening.fruits._\n",
      "       ^cmd2.sc:4: not found: value Planted\n",
      "        for (fruit <- Planted) {\n",
      "                      ^cmd2.sc:5: not found: value showfruit\n",
      "            showfruit(fruit)\n",
      "            ^Compilation Failed"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Compilation Failed"
     ]
    }
   ],
   "source": [
    "import gardening.fruits._\n",
    "object PrintPlanted {\n",
    "    def main(args:Array[String]):Unit ={\n",
    "        for (fruit <- Planted) {\n",
    "            showfruit(fruit)\n",
    "        }\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "voluntary-forth",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
